package com.augurit.common.system.convert;

import java.util.ArrayList;
import java.util.List;

import com.augurit.common.base.extmodel.inner.GroupTreeNode;
import com.augurit.common.base.extmodel.inner.OrgTreeNode;
import com.augurit.common.constant.OrgConstant;
import com.augurit.common.entity.OmUser;
import com.augurit.common.syscode.web.form.SysCodeForm;
import com.augurit.common.system.util.OrgUtils;
import com.augurit.common.system.web.org.form.OmUserForm;

public class OmUserConverter {

	public static void convertToEntity(OmUserForm form, OmUser entity){
		if(form != null && entity != null){

			if(form.getUserId() != null)
				entity.setUserId(form.getUserId());
			
			if(form.getLoginName() != null && form.getLoginName().trim().length() > 0)
				entity.setLoginName(form.getLoginName());
			
			if(form.getPassword() != null && form.getPassword().trim().length() > 0)
				entity.setPassword(form.getPassword());
			
			if(form.getUserCode() != null && form.getUserCode().trim().length() > 0)
				entity.setUserCode(form.getUserCode());
			
			if(form.getUserName() != null && form.getUserName().trim().length() > 0)
				entity.setUserName(form.getUserName());
			
			if(form.getCertMode() != null && form.getCertMode().trim().length() > 0)
				entity.setCertMode(form.getCertMode());
			
			if(form.getCssStyle() != null && form.getCssStyle().trim().length() > 0)
				entity.setCssStyle(form.getCssStyle());
			
			if(form.getSortNo() != null)
				entity.setSortNo(form.getSortNo());
			
			if(form.getIsActive() != null && form.getIsActive().trim().length() > 0)
				entity.setIsActive(form.getIsActive());
			
			if(form.getPosition() != null && form.getPosition().trim().length() > 0)
				entity.setPosition(form.getPosition());
			
			if(form.getSex() != null && form.getSex().trim().length() > 0)
				entity.setSex(form.getSex());
			
			if(form.getTitle() != null && form.getTitle().trim().length() > 0)
				entity.setTitle(form.getTitle());
			
			if(form.getOfficeTel() != null && form.getOfficeTel().trim().length() > 0)
				entity.setOfficeTel(form.getOfficeTel());
			
			if(form.getMobile() != null && form.getMobile().trim().length() > 0)
				entity.setMobile(form.getMobile());
			
			if(form.getMsn() != null && form.getMsn().trim().length() > 0)
				entity.setMsn(form.getMsn());
			
			if(form.getEmail() != null && form.getEmail().trim().length() > 0)
				entity.setEmail(form.getEmail());
			
			if(form.getQq() != null && form.getQq().trim().length() > 0)
				entity.setQq(form.getQq());
			
			if(form.getRtx() != null && form.getRtx().trim().length() > 0)
				entity.setRtx(form.getRtx());
			
			if(form.getSeatNo() != null && form.getSeatNo().trim().length() > 0)
				entity.setSeatNo(form.getSeatNo());
			
			if(form.getDirectorUserId() != null)
				entity.setDirectorUserId(form.getDirectorUserId());
			
			if(form.getStartDate() != null)
				entity.setStartDate(form.getStartDate());
			
			if(form.getEndDate() != null)
				entity.setEndDate(form.getEndDate());
			
			if(form.getCreator() != null && form.getCreator().trim().length() > 0)
				entity.setCreator(form.getCreator());
			
			if(form.getCreateTime() != null)
				entity.setCreateTime(form.getCreateTime());
			
			if(form.getUpdator() != null && form.getUpdator().trim().length() > 0)
				entity.setUpdator(form.getUpdator());
			
			if(form.getUpdateTime() != null)
				entity.setUpdateTime(form.getUpdateTime());
			
			if(form.getRemark() != null && form.getRemark().trim().length() > 0)
				entity.setRemark(form.getRemark());
		}
	}
	
	public static OmUserForm convertToForm(OmUser entity){
		if(entity != null){
			OmUserForm form = new OmUserForm();

			form.setUserId(entity.getUserId());
			form.setLoginName(entity.getLoginName());
			form.setPassword(entity.getPassword());
			form.setUserCode(entity.getUserCode());
			form.setUserName(entity.getUserName());
			form.setCertMode(entity.getCertMode());
			form.setCssStyle(entity.getCssStyle());
			form.setSortNo(entity.getSortNo());
			form.setIsActive(entity.getIsActive());
			form.setPosition(entity.getPosition());
			form.setSex(entity.getSex());
			form.setTitle(entity.getTitle());
			form.setOfficeTel(entity.getOfficeTel());
			form.setMobile(entity.getMobile());
			form.setMsn(entity.getMsn());
			form.setEmail(entity.getEmail());
			form.setQq(entity.getQq());
			form.setRtx(entity.getRtx());
			form.setSeatNo(entity.getSeatNo());
			form.setDirectorUserId(entity.getDirectorUserId());
			form.setStartDate(entity.getStartDate());
			form.setEndDate(entity.getEndDate());
			form.setCreator(entity.getCreator());
			form.setCreateTime(entity.getCreateTime());
			form.setUpdator(entity.getUpdator());
			form.setUpdateTime(entity.getUpdateTime());
			form.setRemark(entity.getRemark());
			
			return form;
		}else
			return null;
	}
	
	/**
	 * 将用户Form对象转换为OrgTreeNode对象
	 * @param form
	 * @return
	 */
	public static OrgTreeNode convertToOrgTreeNode(OmUserForm form, Long orgId, Long posId){
		if(form != null){
			OrgTreeNode node = new OrgTreeNode();
			node.setPartyType(OrgConstant.PARTY_TYPE_USER);
			node.setUserId(form.getUserId());
			node.setOrgId(orgId);
			node.setPosId(posId);
			node.setText(form.getUserName());
			node.setQtip(form.getUserCode());
			node.setIconCls(form.getSex() != null && form.getSex().equals(OrgConstant.SEX_FEMALE) ? OrgTreeNode.CLS_USER_FEMALE : OrgTreeNode.CLS_USER_MALE);
			node.setLeaf(true);
			
			node.setCanManager(true);
			return node;
		}else
			return null;
	}
	
	public static List<OmUserForm> convertToFormList(List<OmUser> list){
		return convertToFormList(list, null, null);
	}
	
	public static List<OmUserForm> convertToFormList(List<OmUser> list,
			List<SysCodeForm> positionList, List<SysCodeForm> sexList){
		
		if(list != null && list.size() > 0 ){
			List<OmUserForm> result = new ArrayList();
			
			for(OmUser entity : list){
				OmUserForm form = convertToForm(entity);
				
				//扩展属性转换
				form.setPositionLabel(OrgUtils.getItemName(form.getPosition(), positionList));
				form.setSexLabel(OrgUtils.getItemName(form.getSex(), sexList));
				
				result.add(form);
			}
			
			return result;
		}
		return null;
	}
	
/**********************工作组树GroupTree******************/
	/**
	 * 将用户Form对象转换为GroupTreeNode对象
	 * @param form
	 * @return
	 */
	public static GroupTreeNode convertToGroupTreeNode(OmUserForm form){
		if(form != null){
			GroupTreeNode node = new GroupTreeNode();
		//	node.setId(OrgTreeNode.getKey(OmUser.class.getSimpleName(), form.getUserId()));
			node.setText(form.getUserName());
			node.setQtip(form.getUserCode());
			node.setIconCls(form.getSex() != null && form.getSex().equals(OrgConstant.SEX_FEMALE) ? OrgTreeNode.CLS_USER_FEMALE : OrgTreeNode.CLS_USER_MALE);
			node.setLeaf(true);
			return node;
		}else
			return null;
	}
 
	
}
